package com.wskj.web.controller.system;

import java.util.List;
import java.util.Arrays;

import com.wskj.system.bo.SysCommonAddBo;
import com.wskj.system.bo.SysCommonEditBo;
import com.wskj.system.bo.SysCommonQueryBo;
import com.wskj.system.service.ISysCommonService;
import com.wskj.system.vo.web.SysCommonVo;
import lombok.AllArgsConstructor;

import javax.validation.constraints.*;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.multipart.MultipartFile;
import com.wskj.common.annotation.RepeatSubmit;
import com.wskj.common.annotation.Log;
import com.wskj.common.core.controller.BaseController;
import com.wskj.common.core.domain.AjaxResult;
import com.wskj.common.enums.BusinessType;
import com.wskj.common.exception.CustomException;
import com.wskj.common.utils.poi.ExcelUtil;
import com.wskj.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;

/**
 * 系统公共Controller
 *
 * @author wsdz
 * @date 2023-04-10
 */
@Api(value = "系统公共控制器", tags = {"系统公共管理"})
@AllArgsConstructor
@RestController
@RequestMapping("/system/common")
public class SysCommonController extends BaseController {

    private final ISysCommonService iSysCommonService;

    /**
     * 查询系统公共列表
     */
    @ApiOperation("查询系统公共列表")
    @PreAuthorize("@ss.hasPermi('system:common:list')")
    @GetMapping("/list")
    public TableDataInfo<SysCommonVo> list(@Validated SysCommonQueryBo bo) {
        return iSysCommonService.queryPageList(bo);
    }

    /**
     * 导出系统公共列表
     */
    @ApiOperation("导出系统公共列表")
    @PreAuthorize("@ss.hasPermi('system:common:export')")
    @Log(title = "系统公共", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<Void> export(@Validated SysCommonQueryBo bo) {
        List<SysCommonVo> list = iSysCommonService.queryList(bo);
        ExcelUtil<SysCommonVo> util = new ExcelUtil<>(SysCommonVo.class);
        return util.exportExcel(list, "系统公共");
    }

    /**
     * 获取系统公共详细信息
     */
    @ApiOperation("获取系统公共详细信息")
    @PreAuthorize("@ss.hasPermi('system:common:query')")
    @GetMapping("/{commonId}")
    public AjaxResult<SysCommonVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("commonId") Long commonId) {
        return AjaxResult.success(iSysCommonService.queryById(commonId));
    }

    /**
     * 新增系统公共
     */
    @ApiOperation("新增系统公共")
    @PreAuthorize("@ss.hasPermi('system:common:add')")
    @Log(title = "系统公共", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody SysCommonAddBo bo) {
        return toAjax(iSysCommonService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改系统公共
     */
    @ApiOperation("修改系统公共")
    @PreAuthorize("@ss.hasPermi('system:common:edit')")
    @Log(title = "系统公共", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody SysCommonEditBo bo) {
        return toAjax(iSysCommonService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除系统公共
     */
    @ApiOperation("删除系统公共")
    @PreAuthorize("@ss.hasPermi('system:common:remove')")
    @Log(title = "系统公共" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{commonIds}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] commonIds) {
        return toAjax(iSysCommonService.deleteWithValidByIds(Arrays.asList(commonIds), true) ? 1 : 0);
    }

    /**
     * 导入系统公共数据
     */
    @ApiOperation("导入系统公共数据")
    @ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "file")
    @PreAuthorize("@ss.hasPermi('system:common:import')")
    @Log(title = "系统公共" , businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult<Void> importData(MultipartFile file) {

        ExcelUtil<SysCommonVo> util = new ExcelUtil<>(SysCommonVo.class);
        List<SysCommonVo> sysCommonList;
        try {
            sysCommonList = util.importExcel(file.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException("文件解析异常！message：" + e.getMessage());
        }
        String message = iSysCommonService.importSysCommon(sysCommonList);
        return AjaxResult.success(message);

    }

    /**
     * 下载模板
     *
     * @return AjaxResult 模板文件
     */
    @ApiOperation("下载模板")
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate() {
        ExcelUtil<SysCommonVo> util = new ExcelUtil<>(SysCommonVo.class);
        return util.importTemplateExcel("系统公共数据");
    }

    /**
     * 查询系统公共列表
     */
    @ApiOperation("查询系统公共列表")
    @PreAuthorize("@ss.hasPermi('system:common:list')")
    @GetMapping("/all/list")
    public AjaxResult<List<SysCommonVo>> AllList(@Validated SysCommonQueryBo bo) {
        return AjaxResult.success(iSysCommonService.queryList(bo));
    }
}
